CON

SOUND_OUTPUT_PIN = 32  
TEST_DURATION = 800
TEST_PIN = 6
HIGH = 1
LOW = 0

'TONES
GbelowC = 196
A3 = 220
Asharp = 233
B3 = 247
C4 = 262
Csharp = 277
D4 = 294
Dsharp = 311
E4 = 330
F4 = 349
Fsharp = 370
G4 = 392
Gsharp = 415

WHOLE = 1600
HALF = WHOLE / 2
QUARTER = HALF / 2
EIGHTH = QUARTER / 2
SIXTEENTH = EIGHTH / 2


OBJ
DEBUG  : "SerialMirror"  
FMATH  :  "FloatMath"

VAR
byte DEBUG_MODE

PUB Initialize
DEBUG_MODE  := DEBUG#DEBUG_STATE 
DEBUG.Str(STRING("Tone Generator Initialization", DEBUG#CR))

if(DEBUG_MODE)
  DEBUG.Str(STRING("Tone Generator Debug function is ON", DEBUG#CR)) 

return

PUB MainUnitTest  | i

waitcnt(5*clkfreq + cnt) 'Pause 5 seconds
MaryHadaLittleLamb(TEST_PIN)
OdeToJoy(TEST_PIN)
waitcnt(3*clkfreq + cnt) 'Pause 3 seconds  


repeat
  DEBUG.Str(STRING("TONE TEST DURATION"))
  PlayTone(262, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(294, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(330, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(349, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(392, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(440, SOUND_OUTPUT_PIN, TEST_DURATION)
  PlayTone(449, SOUND_OUTPUT_PIN, TEST_DURATION)
  

PUB RocketSounds(pin_output, mode)
''Mode 0 equals start-up state
''Mode 1 equals prograde rocket sounds
''Mode 2 equals retrograde rocket sounds
  
case mode
  0: PlayTone(600, pin_output, SIXTEENTH)  
  1:
  2:

PUB OdeToJoy(pin_output)
  
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(F4, pin_output, QUARTER)
  PlayTone(G4, pin_output, QUARTER)
  PlayTone(G4, pin_output, QUARTER)
  PlayTone(F4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)

  PlayTone(C4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, EIGHTH)
  PlayTone(D4, pin_output, EIGHTH)
  PlayTone(D4, pin_output, HALF)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(F4, pin_output, QUARTER)
  PlayTone(G4, pin_output, QUARTER)
  
  PlayTone(G4, pin_output, QUARTER)
  PlayTone(F4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, EIGHTH)
  PlayTone(C4, pin_output, EIGHTH)
  PlayTone(C4, pin_output, HALF)
   
PUB MaryHadaLittleLamb(pin_output) 
  
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, HALF)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, HALF)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(G4, pin_output, QUARTER)
  PlayTone(G4, pin_output, HALF)

  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, WHOLE)

  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, HALF)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, HALF)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(G4, pin_output, QUARTER)
  PlayTone(G4, pin_output, HALF)

  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(E4, pin_output, QUARTER) 
  PlayTone(D4, pin_output, QUARTER)
  PlayTone(C4, pin_output, WHOLE)  


PUB PlayTone(freqInHz, outputPin, duration) | waitTimeDenom, i, numCycles

waitTimeDenom := freqInHz  * 2   '1/waitTimeDenom =  waitTime (i.e 1/4 = wait 0.25 seconds
DIRA[outputPin]~~
OUTA[outputPin] := 1

numCycles := FMATH.FMul(freqInHz, FMATH.FDiv(duration, 1000))
'DEBUG.dec(numCycles) 
repeat i from 0 to 2*numCycles
  !OUTA[outputPin]
  waitcnt(clkfreq/waitTimeDenom + cnt)